package com.yc.exam.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.exam.entity.Course;
import com.yc.exam.entity.CourseOrder;
import com.yc.exam.entity.User;
import com.yc.exam.service.ICourseOrderService;
import com.yc.exam.service.ICourseService;
import com.yc.exam.service.IUserService;
import com.yc.exam.vo.CourseVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 前端控制器
 *
 * @author 王妍
 * @since 2022-02-20
 */
@RestController
@RequestMapping("/exam/course")
public class CourseController {

  @Autowired private ICourseService courseService;

  @Autowired private IUserService userService;

  @Autowired private ICourseOrderService courseOrderService;
  /** 创建课程 */
  @PostMapping
  public Course createCourse(@RequestBody CourseVo courseInfo) {
    final Course course = new Course();
    BeanUtils.copyProperties(courseInfo, course);
    courseService.save(course);
    return course;
  }

  @GetMapping("/page")
  public Page<CourseVo> getPage(Page<CourseVo> courseVoPage, CourseVo course) {
    return courseService.loadPage(courseVoPage, course);
  }

  @DeleteMapping
  public void delete(@RequestBody Long[] courseIds) {
    courseService.removeBatchByIds(Arrays.asList(courseIds));
  }

  @GetMapping
  public List<Course> list() {
    return courseService.list();
  }

  @GetMapping("subscribed")
  public Page<CourseVo> getSubscribed(
      Principal principal, Page<CourseVo> coursePage, CourseVo course) {
    User user =
        Optional.ofNullable(userService.loadUserByUserName(principal.getName()))
            .orElseThrow(() -> new UsernameNotFoundException("用户名不存在！！"));
    return courseService.getSubscribed(user.getId(), coursePage, course);
  }

  @GetMapping("noSubscribed")
  public Page<CourseVo> getNoSubscribed(
      Principal principal, Page<CourseVo> coursePage, CourseVo course) {
    User user =
        Optional.ofNullable(userService.loadUserByUserName(principal.getName()))
            .orElseThrow(() -> new UsernameNotFoundException("用户名不存在！！"));
    return courseService.getNoSubscribed(user.getId(), coursePage, course);
  }

  /** 课程订阅 */
  @PostMapping("order/{courseId}")
  public void order(Principal principal, @PathVariable("courseId") Long courseId) {
    User user =
        Optional.ofNullable(userService.loadUserByUserName(principal.getName()))
            .orElseThrow(() -> new UsernameNotFoundException("用户名不存在！！"));
    CourseOrder courseOrder = new CourseOrder();
    courseOrder.setCourseId(courseId);
    courseOrder.setStudentId(user.getId());
    courseOrderService.save(courseOrder);
  }

  /** 取消课程订阅 */
  @DeleteMapping("order/{courseId}")
  public void unOrder(Principal principal, @PathVariable("courseId") Long courseId) {
    User user =
        Optional.ofNullable(userService.loadUserByUserName(principal.getName()))
            .orElseThrow(() -> new UsernameNotFoundException("用户名不存在！！"));
    CourseOrder courseOrder = new CourseOrder();
    courseOrderService.remove(
        new LambdaQueryWrapper<CourseOrder>()
            .eq(CourseOrder::getCourseId, courseId)
            .eq(CourseOrder::getStudentId, user.getId()));
  }
}
